package com.ulook.lookplanet.controller;

import com.ulook.lookplanet.bean.TPlanet;
import com.ulook.lookplanet.bean.VMatchingCard;
import com.ulook.lookplanet.common.ExceptionCode;
import com.ulook.lookplanet.common.JsonResult;
import com.ulook.lookplanet.service.MatchingService;
import com.ulook.lookplanet.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 描述：
 *
 * @author L1998
 * @date 2021/5/11
 */
@RestController
@RequestMapping("/matching")
public class MatchingController {

    @Autowired
    private MatchingService service;

    /**
     * 获取推荐列表
     */
    @PostMapping("/getMatchingList")
    public JsonResult<List<VMatchingCard>> getMatchingList(HttpServletRequest request){
        String userId = request.getParameter("userId");
        if(StringUtil.isEmpty(userId)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数不存在");
        }
        List<VMatchingCard> res = service.getMatchingList(Integer.parseInt(userId));
        return new JsonResult<>(res);
    }

    /**
     * 设置所属行星
     */
    @PostMapping("/updatePlanet")
    public JsonResult<TPlanet> updatePlanet(HttpServletRequest request){
        String userId = request.getParameter("userId");
        String planetId = request.getParameter("planetId");
        if(StringUtil.isEmpty(userId) || StringUtil.isEmpty(planetId)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数不存在");
        }
        TPlanet planet = service.updatePlanet(Integer.parseInt(userId), planetId);
        if(planet == null){
            return new JsonResult<>(ExceptionCode.NO_DATA, "数据库异常");
        }
        return new JsonResult<>(planet);
    }

}
